﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ybx_caller.entity
{
    public class ShareBackData
    {
        public Result result { set; get; }
        public class Result
        {
            public string code { set; get; }
            public string msg { set; get; }
        }
        public Body body { set; get; }
        public class Body
        {
            public List<List1> list { set; get; }
            public class List1
            {
                public string area_code { set; get; }
                public string enterprise_id { set; get; }
                public string check_type { set; get; }
                public string service_date { set; get; }
                public string quantity { set; get; }
            }
        }

        private static DataTable MergeTables(params DataTable[] tables)
        {
            DataTable mergedTable = tables[0].Clone();
            foreach (DataTable table in tables)
            {
                foreach (DataRow row in table.Rows)
                {
                    mergedTable.ImportRow(row);
                }
            }
            return mergedTable;
        }

        public static string getData(string b,string e)
        {
            // 住院门诊
            DataTable dd0 = GetDataFromHis.QueryDatabase(b,e);
            // 检验
            DataTable dd1 = GetDataFromRis.QueryDatabase(b,e);
            // 检查
            DataTable dd2 = GetDataFromPacs.QueryDatabase(b, e);

            DataTable dd = MergeTables(dd0, dd1, dd2);

            ShareBackData data1 = new ShareBackData();
            if (dd.Rows.Count > 0)
            {
                ShareBackData.Result result = new ShareBackData.Result();
                result.code = "success";
                result.msg = "操作成功";
                ShareBackData.Body body = new ShareBackData.Body();
                List<ShareBackData.Body.List1> lists = new List<ShareBackData.Body.List1>();
                ShareBackData.Body.List1 list = null;
                foreach (DataRow row in dd.Rows)
                {
                    list = new ShareBackData.Body.List1();
                    list.area_code = row["area_code"].ToString();
                    list.enterprise_id = row["enterprise_id"].ToString();
                    list.check_type = row["check_type"].ToString();
                    list.service_date = row["service_date"].ToString();
                    list.quantity = row["quantity"].ToString();
                    lists.Add(list);
                }
                body.list = lists;
                data1.body = body;
                data1.result = result;
            }else
            {
                ShareBackData.Result result = new ShareBackData.Result();
                result.code = "fail";
                result.msg = "未查询到数据";
                data1.result = result;
                ShareBackData.Body body = new ShareBackData.Body();
                List<ShareBackData.Body.List1> lists = new List<ShareBackData.Body.List1>();
                body.list = lists;
                data1.body = body;
            }           
            return JsonConvert.SerializeObject(data1);
        }
    }
}
